package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqLoginOut;
import com.lanlian.commonlib.bean.db.DBUser;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.uikit.CustomDialog;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/13 14:10
 *     desc   : 个人主页
 *     version: 1.0
 * </pre>
 */
public class MineIndexActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvLoginOut;
    private TextView mTvUserId;
    private TextView mTvUserName;
    private TextView mTvSex;
    private TextView mTvMobile;
    private TextView mTvEmail;
    private TextView mTvUpdatePassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_index, true);
        initView();
        setUserInfo();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("个人主页");
        mTvLoginOut = findViewById(R.id.tv_login_out);
        mTvUserId = findViewById(R.id.tv_user_id);
        mTvUserName = findViewById(R.id.tv_user_name);
        mTvSex = findViewById(R.id.tv_sex);
        mTvMobile = findViewById(R.id.tv_mobile);
        mTvEmail = findViewById(R.id.tv_email);
        mTvUpdatePassword = findViewById(R.id.tv_update_password);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvEmail.setOnClickListener(this);
        mTvMobile.setOnClickListener(this);
        mTvUpdatePassword.setOnClickListener(this);
        mTvLoginOut.setOnClickListener(this);
    }

    /**
     * 设置用户信息
     */
    private void setUserInfo() {
        DBUser dbUser = mDBManager.getUser();
        if (dbUser != null) {
            mTvUserName.setText(dbUser.getRealName());
            mTvUserId.setText(dbUser.getAccountNo());
            if (dbUser.getMale() == 1) {
                mTvSex.setText("男");
            } else {
                mTvSex.setText("女");
            }
            mTvMobile.setText(dbUser.getMobile());
            mTvEmail.setText(dbUser.getMail());
        } else {
            finish();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_bank:
                startActivity(new Intent(this, UpdateBankActivity.class));
                break;
            case R.id.tv_update_password:
                startActivity(new Intent(this, UpdatePasswordActivity.class));
                break;
            case R.id.tv_mobile:
                startActivity(new Intent(this, UpdateMobileActivity.class));
                break;
            case R.id.tv_email:
                startActivity(new Intent(this, UpdateEmailActivity.class));
                break;
            case R.id.tv_login_out:
                new CustomDialog(this, new CustomDialog.OnClickListener() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                        showLoadingDialog("退出登录");
                        VolleyRequestUtils.getInstance().loginOut(MineIndexActivity.this.toString(), new ReqLoginOut(),
                                new INetWorkCallback() {
                                    @Override
                                    public void onSuccess(Object response) {
                                        dismissLoadingDialog();
                                        mDBManager.setLoginOut();
                                        showToast("退出登录成功");
                                        EventBusUtils.post(new EventMessage(EventCode.EVENT_LOGIN_OUT_REFRESH));
                                        finish();
                                    }

                                    @Override
                                    public void onFail(BaseResponse baseResponse) {
                                        dismissLoadingDialog();
                                        showToast(baseResponse.getMsg());
                                    }
                                });
                    }
                }).setTitle("提示").setContent("确定退出登录吗？").show();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUserInfo();
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_LOGIN_OUT_REFRESH:
                finish();
                break;
            default:
                break;
        }
    }
}
